<?php require_once "../autoload.php"; ?>
<?php
    if(isset($_REQUEST['submit'])):
        $objPromocoes = new Promocoes($_POST['data']);
        $retorno = $objPromocoes->save();
    endif;

    if(isset($_REQUEST['promocao'])):

        $promocao = (int) $_REQUEST['promocao'];
        if($promocao > 0):
                $objPromocoes = new Promocoes();
                $objPromocoes->setId($promocao);
                $dadosPromocao = $objPromocoes->find();
        endif;
    endif;



?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Admin</title>
		<link rel="stylesheet" type="text/css" href="/assets/style.css" media="all" />
                
                <link rel="stylesheet" type="text/css" href="/assets/demo_table.css" media="all" />
                <link rel="stylesheet" type="text/css" href="/assets/demo_table_jui.css" media="all" />
                <!-- SCRIPTS -->
                <script type="text/javascript" src="/assets/jquery-1.5.1.min.js"></script>
                <script type="text/javascript" src="/assets/jquery.counter-1.0.min.js"></script>
                <script type="text/javascript" src="/assets/jquery.dataTables.js"></script>
		<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#tbpromocoes').dataTable({
                                    "sPaginationType": "full_numbers"
                                });
                                <?php if(!isset($_REQUEST['promocao'])):?>
                                    $("#form").hide();
                                <?php endif;?>
                                $("#incluir").click(function(){
                                    $('#form').slideToggle('slow');
                                })

                                $("#tweet").counter({
                                    count: 'up',
                                    goal: 140
                                });

			});
                        function deletar(id){
                            var confirma = confirm('Deseja deletar o registro?');
                            if(confirma){
                                $.get('/admin/delete.php',{'id':id},function(data){
                                    if(data == 'ok'){
                                        self.location = '/admin';
                                    }
                                })
                            }
                        }

		</script>

    </head>
    <body>
		
		<h2>Admin do sistema de Promoções</h2>
		
		<div>Promoções:</div>
                <div id="demo">
                                    <table cellpadding="5" cellspacing="2" border="0" id="tbpromocoes">
                                        <thead>
                                                <tr>
                                                        <th>Nome</th>
                                                        <th>Descrição</th>
                                                        <th>Tweet</th>
                                                        <th>Data Inicio</th>
                                                        <th>Data Fim</th>
                                                        <th>Ativo</th>
                                                        <th>Cupom desconto</th>
                                                        <td>Qtd usuários</th>
                                                        <th>Ações</th>

                                                </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                                $objPromocoes = new Promocoes();
                                                $promocoes = $objPromocoes->fetchAll();
                                            ?>
                                            <?php foreach($promocoes as $promocao):?>
                                            <tr>
                                                <td><?php echo $promocao['nome'];?></td>
                                                <td><?php echo $promocao['descricao'];?></td>
                                                <td><?php echo $promocao['tweet'];?></td>
                                                <td><?php echo $promocao['dataInicio'];?></td>
                                                <td><?php echo $promocao['dataFim'];?></td>
                                                <td><?php echo ($promocao['ativo'] ? 'Sim': 'Não');?></td>
                                                <td><?php echo $promocao['cupomDesconto'];?></td>
                                                <td align="center"><?php echo $objPromocoes->getTotalParticipantes($promocao['id']);?></td>
                                                <td>
                                                    <a href="../admin/?promocao=<?php echo $promocao['id'];?>">Editar</a>|<a href="javascript:;" onclick="deletar(<?php echo $promocao['id'];?>);return false;">Excluir</a>
                                                </td>
                                            </tr>
                                            <?php endforeach;?>
                                        </tbody>
                                    </table>
                </div>
		
		
		
		<h2>Cadastrar uma nova promoção</h2>
                <?php if(!isset($_REQUEST['promocao'])):?>
                    <h2 id="incluir" style="cursor: pointer">Incluir</h2>
                <?php endif;?>
		<div id="form">
			<form action="index.php" method="post">
                                <?php if(isset($dadosPromocao['id'])):?>
                                <input type="hidden" name="data[id]" value="<?php echo $dadosPromocao['id']?>"/>
                                <?php endif;?>
				<fieldset>
					<legend>Dados</legend>
                                        <div id="dados">
                                            <div class="left">
                                                <label for="nome">Nome: </label>
                                                <br/>
                                                <input type="text" name="data[nome]" value="<?php echo $dadosPromocao['nome'];?>"/>
                                                <br/>
                                                <label for="descricao">Descrição: </label>
                                                <br/>
                                                <textarea name="data[descricao]" cols="50" rows="5"><?php echo $dadosPromocao['descricao'];?></textarea>
                                                <br/>
                                                <label for="tweet">Tweet: </label>
                                                <br/>
                                                <textarea id="tweet" name="data[tweet]" cols="50" rows="5"><?php echo $dadosPromocao['tweet'];?></textarea>
                                                
                                            </div>
                                            <div class="left">
                                                <label for="dataInicio">Data Inicio: </label>
                                                <br/>
                                                <input type="text" name="data[dataInicio]" value="<?php echo $dadosPromocao['dataInicio'];?>"/>
                                                <br/>
                                                <label for="dataFim">Data Inicio: </label>
                                                <br/>
                                                <input type="text" name="data[dataFim]" value="<?php echo $dadosPromocao['dataFim'];?>"/>
                                                <br/>
                                                <label for="ativo">Ativa: </label>
                                                <br/>
                                                <input type="radio" name="data[ativo]" value="1" <?php if ($dadosPromocao['ativo']==1):?> checked <?php endif;?>>Sim
                                                <input type="radio" name="data[ativo]" value="0" <?php if ($dadosPromocao['ativo']==0):?> checked <?php endif;?>>Não

                                                <br/>
                                                <label for="cupom">Cupom Desconto: </label>
                                                <br/>
                                                <input type="text" name="data[cupomDesconto]" value="<?php echo $dadosPromocao['cupomDesconto'];?>"/>
                                                <br />
                                                <input type="submit" name="submit" value="Gravar" />
                                                <input type="button" name="cancelar" value="Cancelar" onclick="self.location='/admin'" />
                                            </div>
                                        </div>
				</fieldset>		
				
			</form>
		</div>


    </body>
</html>
